﻿/**
*   I don't recommend using this plugin on large tables, I just wrote it to make the demo useable. It will work fine for smaller tables 
*   but will likely encounter performance issues on larger tables.
*
*		<input type="text" class="form-control" id="dev-table-filter" data-action="filter" data-filters="#dev-table" placeholder="Filter Developers" />
*		$(input-element).filterTable()
*		
*	The important attributes are 'data-action="filter"' and 'data-filters="#table-selector"'
*	http://bootsnipp.com/snippets/featured/panel-tables-with-filter
*/
(function () {
	'use strict';
	var $ = jQuery;
	$.fn.extend({
		filterTable: function () {
			return this.each(function () {
				$(this).on('keyup', function (e) {
					$('.filterTable_no_results').remove();
					var $this = $(this),
                        search = $this.val().toLowerCase(),
                        target = $this.attr('data-filters'),
                        $target = $(target),
                        $rows = $target.find('tbody tr');

					if (search == '') {
						$rows.show();
					} else {
						$rows.each(function () {
							var $this = $(this);
							$this.text().toLowerCase().indexOf(search) === -1 ? $this.hide() : $this.show();
						})
						if ($target.find('tbody tr:visible').length === 0) {
						    var col_count = $target.find('tr').first().find('td').length;
							var no_results = $('<tr class="filterTable_no_results"><td colspan="' + col_count + '">Nenhum registro encontrado!</td></tr>')
							$target.find('tbody').append(no_results);
						}
					}
				});
			});
		}
	});
	$('[data-action="filter"]').filterTable();
})(jQuery);